<template>
  <label 
    :class="[
      'bx-checkbox',
      {
        'bx-checkbox--checked': isChecked,
        'bx-checkbox--disabled': disabled,
        'bx-checkbox--indeterminate': indeterminate
      }
    ]"
  >
    <span class="bx-checkbox__input">
      <input
        ref="inputRef"
        v-model="checkboxValue"
        type="checkbox"
        :disabled="disabled"
        :value="value"
        class="bx-checkbox__original"
        @change="handleChange"
      />
      <span class="bx-checkbox__inner bx-glass">
        <BxIcon 
          v-if="indeterminate" 
          name="minus" 
          class="bx-checkbox__icon"
        />
        <BxIcon 
          v-else-if="isChecked" 
          name="check" 
          class="bx-checkbox__icon"
        />
      </span>
    </span>
    <span v-if="$slots.default || label" class="bx-checkbox__label">
      <slot>{{ label }}</slot>
    </span>
  </label>
</template>

<script>
import { ref, computed, inject } from 'vue'
import BxIcon from '../basic/BxIcon.vue'

export default {
  name: 'BxCheckbox',
  components: {
    BxIcon
  },
  props: {
    modelValue: {
      type: [Boolean, Array],
      default: false
    },
    value: {
      type: [String, Number, Boolean],
      default: undefined
    },
    label: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    indeterminate: {
      type: Boolean,
      default: false
    },
    trueValue: {
      type: [String, Number, Boolean],
      default: true
    },
    falseValue: {
      type: [String, Number, Boolean],
      default: false
    }
  },
  emits: ['update:modelValue', 'change'],
  setup(props, { emit }) {
    const inputRef = ref(null)
    const checkboxGroup = inject('bx-checkbox-group', null)

    const isGroup = computed(() => !!checkboxGroup)

    const checkboxValue = computed({
      get() {
        if (isGroup.value) {
          return checkboxGroup.modelValue.includes(props.value)
        }
        return props.modelValue === props.trueValue
      },
      set(value) {
        if (isGroup.value) {
          const newValue = [...checkboxGroup.modelValue]
          if (value) {
            if (!newValue.includes(props.value)) {
              newValue.push(props.value)
            }
          } else {
            const index = newValue.indexOf(props.value)
            if (index > -1) {
              newValue.splice(index, 1)
            }
          }
          checkboxGroup.changeEvent(newValue)
        } else {
          const newValue = value ? props.trueValue : props.falseValue
          emit('update:modelValue', newValue)
          emit('change', newValue)
        }
      }
    })

    const isChecked = computed(() => {
      if (isGroup.value) {
        return checkboxGroup.modelValue.includes(props.value)
      }
      return props.modelValue === props.trueValue
    })

    const handleChange = (event) => {
      if (props.disabled) return
      
      const value = event.target.checked
      checkboxValue.value = value
    }

    return {
      inputRef,
      checkboxValue,
      isChecked,
      handleChange
    }
  }
}
</script>

<style scoped>
.bx-checkbox {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  font-family: var(--bx-font-family);
  user-select: none;
  transition: all var(--bx-transition-medium);
}

.bx-checkbox:hover .bx-checkbox__inner {
  border-color: var(--bx-glass-border-hover);
  background: var(--bx-glass-bg-hover);
}

.bx-checkbox--disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.bx-checkbox__input {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.bx-checkbox__original {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
}

.bx-checkbox__inner {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border: 1px solid var(--bx-glass-border);
  border-radius: var(--bx-radius-small);
  background: var(--bx-glass-bg);
  backdrop-filter: var(--bx-glass-blur);
  transition: all var(--bx-transition-medium);
}

.bx-checkbox--checked .bx-checkbox__inner {
  background: rgba(0, 122, 255, 0.25);
  border-color: rgba(0, 122, 255, 0.5);
}

.bx-checkbox--indeterminate .bx-checkbox__inner {
  background: rgba(0, 122, 255, 0.25);
  border-color: rgba(0, 122, 255, 0.5);
}

.bx-checkbox__icon {
  font-size: 12px;
  color: var(--bx-text-primary);
  opacity: 0;
  transform: scale(0);
  transition: all var(--bx-transition-fast);
}

.bx-checkbox--checked .bx-checkbox__icon,
.bx-checkbox--indeterminate .bx-checkbox__icon {
  opacity: 1;
  transform: scale(1);
}

.bx-checkbox__label {
  margin-left: 8px;
  color: var(--bx-text-primary);
  font-size: 14px;
  line-height: 1.5;
}

.bx-checkbox--disabled .bx-checkbox__label {
  color: var(--bx-text-tertiary);
}

/* 焦点样式 */
.bx-checkbox__original:focus + .bx-checkbox__inner {
  box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.1);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .bx-checkbox__inner {
    width: 16px;
    height: 16px;
  }
  
  .bx-checkbox__icon {
    font-size: 10px;
  }
  
  .bx-checkbox__label {
    font-size: 13px;
    margin-left: 6px;
  }
}
</style>

